package org.iridium.swinggtalk.friendlist;

import java.util.Collection;
import javax.swing.DefaultListModel;
import javax.swing.JDialog;
import org.iridium.swinggtalk.chat.ChatDialog;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.XMPPConnection;

/**
 *
 * @author iridiumcao
 */
public class FriendListPanel extends javax.swing.JPanel {

    XMPPConnection connection = null;
    DefaultListModel friendListModel = new DefaultListModel();

    /** Creates new form FriendListPanel */
    public FriendListPanel() {
        initComponents();
    }

    public FriendListPanel(XMPPConnection connection) {
        this();
        this.connection = connection;

        Roster roster = connection.getRoster();
        Collection<RosterEntry> entries = roster.getEntries();
        for (RosterEntry entry : entries) {
            // System.out.println(entry.getUser());
            friendListModel.addElement(entry.getUser());
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        friendList = new javax.swing.JList();

        setName("Form"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        friendList.setModel(friendListModel);
        friendList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        friendList.setName("friendList"); // NOI18N
        friendList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                friendListMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(friendList);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 225, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 333, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void friendListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_friendListMouseClicked
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                String toUid = (String) (friendList.getSelectedValue());
                ChatManager chatManager = connection.getChatManager();
                JDialog chatDialog = new ChatDialog(null, false, toUid, chatManager);
                chatDialog.setVisible(true);
//                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
//
//                    @Override
//                    public void windowClosing(java.awt.event.WindowEvent e) {
//                        System.exit(0);
//                    }
//                });

            }
        });
    }//GEN-LAST:event_friendListMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList friendList;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
